/*
 * @Author: yangmiaomiao
 * @Date: 2021-10-26 15:01:05
 * @LastEditors: duchengdong
 * @LastEditTime: 2021-12-28 21:32:26
 * @Description:
 */
import axios, { AxiosRequestConfig } from 'axios'
import { ResConfig, ResObj } from 'types/config.type'
import { getStorage } from 'utils/localStorage'
import { message } from 'antd'

const API_PATH = process.env.REACT_APP_API_PATH

axios.defaults.validateStatus = () => true
axios.defaults.timeout = 60000
axios.defaults.baseURL = API_PATH
// 请求前
axios.interceptors.request.use(
    (config) => {
        // 获取token数据
        const token = getStorage('token')
        // 设置请求头
        config.headers = config.headers || {}
        // 设置参数添加token
        config.headers.Authorization = token || ''
        config.headers['Content-Type'] = 'application/json;charset=UTF-8'
        return config
    },
    (error) => {},
)

// 请求后
axios.interceptors.response.use(
    (config) => {
        return config
    },
    (error) => {},
)
//eslint-disable-next-line
export default (config: AxiosRequestConfig) =>
    new Promise<ResConfig<ResObj>>((resolve) => {
        axios(config)
            .then((res: any) => {
                if (res?.data?.code === 401) {
                    //登录状态已过期
                    message.error(res?.data?.msg || '请求失败')
                    if (!window.location.href.includes('localhost')) {
                        
                    }
                } else {
                    resolve(res?.data || {})
                }
            })
            .catch(() => {})
    })
